package cn.alone.dictionary.controller;

import cn.alone.basic.util.AjaxResult;
import cn.alone.basic.util.PageList;
import cn.alone.dictionary.domain.SystemDictionaryType;
import cn.alone.dictionary.query.SystemDictionaryTypeQuery;
import cn.alone.dictionary.service.ISystemDictionaryDetailService;
import cn.alone.dictionary.service.ISystemDictionaryTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 数据字典
 * @author lanan
 */
@RestController
@RequestMapping("/obtain")
public class SystemDictionaryTypeController {

    /**
     * 新增或修改
     */
    @Autowired
    private ISystemDictionaryTypeService systemDictionaryTypeService;
    @Autowired
    private ISystemDictionaryDetailService systemDictionaryDetailService;
    @PostMapping("/save")
    public AjaxResult save(@RequestBody SystemDictionaryType systemDictionaryType){
        try {
            if(systemDictionaryType.getId()==null){
                systemDictionaryTypeService.add(systemDictionaryType);
            }else {
                systemDictionaryTypeService.update(systemDictionaryType);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作失败").setSuccess(false);
        }
    }
    //删除

    /**
     * /{id}直接接收前端传过来的参数
     * @PathVariable取值
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult removeById(@PathVariable("id") Long id){
        try {
            systemDictionaryTypeService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }

    }

    /**
     * 根据id查询一条数据
     * @return
     */
    @GetMapping("/{id}")
    public AjaxResult findById(@PathVariable("id") Long id){
        try {
            systemDictionaryTypeService.loadById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }

    /**
     * 获取所有数据
     * @return
     */
    @GetMapping("/list")
    public List<SystemDictionaryType> loadAll(){
        return systemDictionaryTypeService.loadAll();
    }


    @PostMapping("/page")
    public PageList<SystemDictionaryType> querPage(@RequestBody SystemDictionaryTypeQuery query){
        return systemDictionaryTypeService.querpage(query);
    }


}
